import { Component, OnInit,OnDestroy,HostListener  } from '@angular/core';
import { Router, ActivatedRoute, NavigationEnd , ParamMap } from '@angular/router';
import { TranslateService } from "@ngx-translate/core";
import { HttpModule } from '@angular/http';
declare var $: any;
declare var QRCode: any;
import SysData from '../shared/sysData';
import storage from '../shared/storage';
import Base64, {default as base64} from '../shared/base64';
import {LoginService} from './login.service';
import { NzMessageService ,UploadFile} from 'ng-zorro-antd';
import tools from '../shared/tools';
import { HttpClient, HttpRequest, HttpResponse } from '@angular/common/http';
import { filter } from 'rxjs/operators';
@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
    isSpinning:boolean=false;
    languageList=[];
    selectedLanguage:any;
    deviceList=[];
    selectDevice=null;
    constructor(
              private router: Router,
              private loginService:LoginService,
              private message: NzMessageService,
              private translateService:TranslateService,
              private http:HttpClient) { }

    //登录
    submitForm(){
        // let thisDevice=this.deviceList.find(item=>{
        //     return item['value']==this.selectDevice;
        // });
        // let LoginForm={
        //     type:thisDevice.value,
        //     name:thisDevice.name
        // };
        // let data={LoginForm};
        this.isSpinning=true;
        this.loginService.login().subscribe(res =>{
            this.isSpinning=false;
            console.log(res);
            if(res.status==1){
               storage.setSessionStorage('LoginForm',res.data);
               this.router.navigate(['../home']);
            }else {
                this.message.error(res.message);
            }
        },error=>{
            this.isSpinning=false;
            this.message.error(error);
        });
    }
   //切换语言并缓存,翻译
    languageChange(item){
        this.selectedLanguage=item.value;
        this.translateService.setDefaultLang(this.selectedLanguage);
        this.translateService.use(this.selectedLanguage);
        storage.set("defaultLang",this.selectedLanguage);
        this.translateService.get('ele_syst_eas').subscribe(value => {
            document.getElementsByTagName("title")[0].innerHTML=value;
        });
        // this.getAllDevice();
    }
   //获取语言列表
   getAllLanguage(){
    this.loginService.getAllLanguage().subscribe(res =>{
        console.log(res);
        if(res.status){
            let data=res.data;
            this.languageList=Object.entries(data).map(item=>{
                let item1={value:item[0],name:item[1]};
                return item1;
            });
            let defaultLanguage=this.languageList.find(data=>{
                return storage.get("defaultLang")==data.value
            })['value'];
            this.selectedLanguage=defaultLanguage?defaultLanguage:this.languageList[0].value;
            storage.set("defaultLang",this.selectedLanguage);
            // this.getAllDevice();
        }else {
            this.message.error(res.message);
        }
    },error=>{
      this.message.error(error);
    });
  }
    //获取设备列表
    getAllDevice(){
        this.deviceList=[];
        this.loginService.getAllDevice().subscribe(res =>{
            if(res.status==1){
                let data=res.data;
                this.deviceList=Object.entries(data).map(item=>{
                    let item1={value:item[0],name:item[1]};
                    return item1;
                });
                this.selectDevice=this.deviceList[0].value;
            }else {
                this.message.error(res.message);
            }
        },error=>{
            this.message.error(error);
        });
    }
    //帮助
    help(){
        this.message.warning('no-help');
    }
    ngOnInit() {
        //事先清空所有缓存，防止浏览器直接回到登录页面
        storage.clearSessionStorage();
        console.log(storage.get("defaultLang"));
        //=====登录页面页面开启======
        if(storage.get("defaultLang")){
            //从缓存取出语言并翻译
            this.translateService.setDefaultLang(storage.get("defaultLang"));
            this.translateService.use(storage.get("defaultLang"));
            this.translateService.get('ele_syst_eas').subscribe(value => {
                document.getElementsByTagName("title")[0].innerHTML=value;
            });
        } else {
            //从配置页面获取默认语言并且翻译/缓存
            this.http.get("assets/i18n/lang.json").subscribe(res=>{
                let lang=res['lang'];
                this.translateService.setDefaultLang(lang);
                this.translateService.use(lang);
                storage.set("defaultLang",lang);
                this.translateService.get('ele_syst_eas').subscribe(value => {
                    document.getElementsByTagName("title")[0].innerHTML=value;
                });
            });
        }
        this.getAllLanguage();
    }
}
